package logic

import (
	"context"
	"fmt"
	"tiktok/model/mysql"
	cache "tiktok/model/redis"

	"tiktok/tiktokrpc/internal/svc"
	"tiktok/tiktokrpc/tiktokrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *tiktokrpc.RegisterRequest) (*tiktokrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	get := cache.Redis.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SmsCode {
		return nil, fmt.Errorf("验证码输入错误")
	}

	user := mysql.TikTokUser{
		Mobile:   in.Mobile,
		Password: in.Mobile,
	}
	mysql.DB.Debug().Create(&user)

	return &tiktokrpc.RegisterResponse{}, nil
}
